package com.tcl.logoidentify.commons;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class TestUtils {
	private static ConcurrentMap<String, Long> longCounter = new ConcurrentHashMap<String, Long>();
	private static ConcurrentMap<String, TimeCounter> timeCounter = new ConcurrentHashMap<String, TimeCounter>();

	public static void timeStart(String key) {
		timeCounter.put(key, new TimeCounter().setStart(System.currentTimeMillis()));
	}

	public static long timeEnd(String key) {
		TimeCounter tc = timeCounter.remove(key);
		return tc.setEnd(System.currentTimeMillis()).getDuration();
	}

	public static Long getTimeDuration(String key) {
		return timeCounter.get(key).getDuration(System.currentTimeMillis());
	}

	public static long counterIncrement(String key, long n) {
		Long counter = longCounter.get(key);
		if (counter != null) {
			counter += n;
			longCounter.replace(key, counter);
		} else {
			counter = n;
			longCounter.put(key, counter);
		}
		return counter;
	}

	public static long endCounter(String key) {
		Long counter = longCounter.get(key);
		if (counter != null) {
			longCounter.remove(key);
		}
		return counter;
	}

	private static class TimeCounter {
		private Long start = null;
		private Long end = null;

		public TimeCounter setStart(Long start) {
			this.start = start;
			return this;
		}

		public TimeCounter setEnd(Long end) {
			this.end = end;
			return this;
		}

		public Long getDuration() {
			return end - start;
		}
		public Long getDuration(Long end) {
			return end - start;
		}

	}
}
